<template>
  <div class="box">
    <h2>
      轮播图管理页面<small
        ><el-button type="primary" size="small" round @click="showDialog()"
          >新增轮播图</el-button
        ></small
      >
    </h2>
    <el-table height="600" :data="banner.bannerList" row-key="id" style="100%">
      <el-table-column prop="id" label="序号" width="80"> </el-table-column>
      <el-table-column prop="title" label="图片名称" width="150">
      </el-table-column>
      <el-table-column prop="img" label="轮播图片">
        <template v-slot="props">
          <el-avatar
            v-if="props.row.pid !== 0"
            shape="square"
            :size="100"
            fit="cover"
            :src="'http://localhost:3000' + props.row.img"
          ></el-avatar>
        </template>
      </el-table-column>
      <el-table-column prop="status" label="状态" width="150">
        <template v-slot="props">
          <el-tag type="success" size="small" v-if="props.row.status === 1"
            >启用</el-tag
          >
          <el-tag type="danger" size="small" v-else>禁用</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" fixed="right" width="180">
        <template v-slot="props">
          <el-button
            type="success"
            size="small"
            round
            @click="editBanner(props.row)"
            >编辑</el-button
          >
          <el-button
            type="danger"
            size="small"
            round
            @click="delBanner(props.row)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>
    <Dialog ref="VDialog"></Dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from "vue";
import { ElMessage, ElMessageBox } from "element-plus";
import { useRouter } from "vue-router";
// 导入store
import useBannerStore from "../../stores/banners";
// 导入弹窗组件
import Dialog from "./dialog.vue";

const banner = useBannerStore();

const $router = useRouter();

// 获取数据
onMounted(() => {
  // 调用函数,获取分类数据
  banner.getBannerListAction();
});

// 弹窗变量
const VDialog = ref(null);

// 打开弹窗
const showDialog = () => {
  VDialog.value.dialogVisible = true;
  // console.log(VDialog.value.dialogVisible);
};
//删除轮播图
function delBanner(Banner) {
  // console.log("Banner", Banner);
  ElMessageBox.confirm("确定删除此轮播图吗？", "Warning", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  })
    .then(() => {
      // 调用删除函数
      banner.delBannerAction(Banner.id);
    })
    .catch(() => {
      ElMessage({
        type: "info",
        message: "用户取消了删除此轮播图",
      });
    });
}
// 编辑轮播图
function editBanner(Banner) {
  // console.log("11111111111111", VDialog.value, Banner.id);
  // 显示弹窗，将数据发送给子组件
  VDialog.value.acceptValue(Banner);
  showDialog();
}
</script>

<style lang="css" scoped></style>
